package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.CouponQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Coupon;
import cn.org.eshow.service.CouponManager;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = ""),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"search","delete","view","update","save","distribution"})
public class CouponAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private CouponManager couponManager;

    private List<Coupon> coupons;
    private Coupon coupon = new Coupon();
    private CouponQuery query=new CouponQuery();

    private String userIds;//用户IDs(逗号隔开)

    public String search() {
        Page<Coupon> page = couponManager.search(query);
        coupons = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query",query);
        return LIST;
    }

    public void delete() {
        Coupon old = couponManager.get(id);
        old.setEnabled(Boolean.FALSE);
        couponManager.save(old);
        saveMessage("删除成功");
        RenderUtil.success("删除成功");
    }

    public String view() {
        if (id != null) {
            coupon = couponManager.get(id);
        }
        return NONE;
    }

    public String update() throws Exception {
        Coupon old = couponManager.get(id);
        old.setTitle(coupon.getTitle() != null ? coupon.getTitle() : old.getTitle());
        old.setStartTime(coupon.getStartTime() != null ? coupon.getStartTime() : old.getStartTime());
        old.setEndTime(coupon.getEndTime() != null ? coupon.getEndTime() : old.getEndTime());
        old.setMoney(coupon.getMoney()!= null ? coupon.getMoney() : old.getMoney());
        old.setScope(coupon.getScope()!= null ? coupon.getScope() : old.getScope());
        old.setRequirement(coupon.getRequirement()!= null ? coupon.getRequirement() : old.getRequirement());
        old.setTotal(coupon.getTotal()!= null ? coupon.getTotal() : old.getTotal());
        old.setStock(coupon.getStock()!= null ? coupon.getStock() : old.getStock());
        old.setState(coupon.getState()!=null?coupon.getState():old.getState());

        couponManager.save(old);
        saveMessage("修改成功");
        return REDIRECT;
    }

    public String save() throws Exception {
        coupon.setState(0);
        coupon.setStock(coupon.getTotal());
        coupon.setUsed(0);
        couponManager.save(coupon);
        saveMessage("添加成功");
        return REDIRECT;
    }

    /**
     * 分配红包给指向用户ids
     */
    public void distribution(){
        if(id==null || StringUtils.isEmpty(userIds)){
            RenderUtil.failure("缺少参数");
            return;
        }

        Coupon old=couponManager.get(id);
        couponManager.distribution(old,userIds);
        saveMessage("分配成功");
        RenderUtil.success("分配成功");
    }


    public List<Coupon> getCoupons() {
        return coupons;
    }

    public void setCoupons(List<Coupon> coupons) {
        this.coupons = coupons;
    }

    public Coupon getCoupon() {
        return coupon;
    }

    public void setCoupon(Coupon coupon) {
        this.coupon = coupon;
    }

    public CouponQuery getQuery() {
        return query;
    }

    public void setQuery(CouponQuery query) {
        this.query = query;
    }

    public String getUserIds() {
        return userIds;
    }

    public void setUserIds(String userIds) {
        this.userIds = userIds;
    }
}